﻿using AutoMapper;
using ProjectManageSystemApi.DbEntity;
using ProjectManageSystemApi.Enums;
using ProjectManageSystemApi.Models;
using System;
using System.Collections.Generic;

namespace ProjectManageSystemApi.Services
{
    public class SupplierGoodsService : ISupplierGoodsService
    {
        readonly IFreeSql _fsql;
        readonly IMapper _mapper;
        public SupplierGoodsService(IFreeSql fsql, IMapper mapper)
        {
            _fsql = fsql;
            _mapper = mapper;
        }
        public bool Create(SupplierGoodsInfoDto supplierGoodsInfo, long userId)
        {
            var info = _mapper.Map<SupplierGoodsInfo>(supplierGoodsInfo);
            info.CreateTime = DateTime.Now;
            info.CreateUser = userId;
            info.State = (sbyte)EnumSupplierGoodsInfo.Nomal;
            return _fsql.Insert<SupplierGoodsInfo>(info).ExecuteIdentity() > 0;
        }
        public bool Update(SupplierGoodsInfoDto supplierGoodsInfo)
        {
            var data = _mapper.Map<SupplierGoodsInfo>(supplierGoodsInfo);
            return _fsql.Update<SupplierGoodsInfo>()
                .SetSource(data)
                .UpdateColumns(u => new { u.GoodsName, u.GoodsPrice, u.DeliveryCycle, u.OfferDate })
                .ExecuteAffrows() > 0;
        }

        public bool Delete(int id)
        {
            return _fsql.Update<SupplierGoodsInfo>(id)
                .Set(x => x.State, (sbyte)EnumSupplierGoodsInfo.Delete)
                .ExecuteAffrows() > 0;
        }

        public bool ExistById(int id)
        {
            return _fsql.Select<SupplierGoodsInfo>().Any(p => p.Id == id && p.State == (sbyte)EnumSupplierGoodsInfo.Nomal);
        }
        
        public bool ExistBySupplierIdAndGoods(int supplierId, string name, int id = 0)
        {
            return _fsql.Select<SupplierGoodsInfo>().Any(p => p.SupplierId == supplierId && p.State == (sbyte)EnumSupplierGoodsInfo.Nomal && p.GoodsName == name && p.Id != id);
        }

        public Models.PageVo<IEnumerable<SupplierGoodsInfoDto>> GetAll(Models.PageBo<SupplierGoodsParams> pageBo)
        {
            var datas = _fsql.Select<SupplierGoodsInfo>()
                .Include(x=>x.UserInfo)
                .Where(p => p.State == (sbyte)EnumSupplierGoodsInfo.Nomal);
            if (pageBo.Param.SupplierId != 0)
            {
                datas = datas.Where(d => d.SupplierId == pageBo.Param.SupplierId);
            }
            if (!string.IsNullOrWhiteSpace(pageBo.Param.SupplierGoodsName))
            {
                datas = datas.Where(d => d.GoodsName.Contains(pageBo.Param.SupplierGoodsName));
            }
            var total = datas.Count();
            datas = datas.Page(pageBo.Page, pageBo.Size);

            var data = new Models.PageVo<IEnumerable<SupplierGoodsInfoDto>>
            {
                Page = pageBo.Page,
                Size = pageBo.Size,
                Datas = _mapper.Map<List<SupplierGoodsInfoDto>>(datas.ToList()),
                Total = total
            };
            return data;
        }

    }
}
